/*
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include "postid.h"
#include "threadid.h"
#include <QHash>

namespace Wishmaster
{
	PostId::PostId() :
		m_threadId(0),
		m_postId(0),
		m_attachIndex(0),
		m_valid(false)
	{
	}

	PostId::PostId(const ThreadId& threadId, unsigned long postId) :
			m_chan(threadId.chanName()),
			m_board(threadId.boardId()),
			m_threadId(threadId.threadNumericId()),
			m_postId(postId),
			m_attachIndex(0),
			m_valid(true)
	{

	}

	PostId::PostId(const QString& chan, const QString& board, unsigned long threadId, unsigned long postId, int attach) :
		m_chan(chan),
		m_board(board),
		m_threadId(threadId),
		m_postId(postId),
		m_attachIndex(attach),
		m_valid(true)
	{
	}

	PostId::PostId(const PostId& id, int attach) :
		m_chan(id.chan()),
		m_board(id.board()),
		m_threadId(id.threadId()),
		m_postId(id.postId()),
		m_attachIndex(attach),
		m_valid(true)
	{
	}
	
	PostId::~PostId()
	{
	}

	QString PostId::chan() const
	{
		return m_chan;
	}

	QString PostId::board() const
	{
		return m_board;
	}

	unsigned long PostId::threadId() const
	{
		return m_threadId;
	}

	unsigned long PostId::postId() const
	{
		return m_postId;
	}

	int PostId::attachIndex() const
	{
		return m_attachIndex;
	}

	bool PostId::isValid() const
	{
		return m_valid;
	}

	QString PostId::toString() const
	{
		return m_chan + m_board + QString::number(m_threadId) + '/' + QString::number(m_postId) + '/' + QString::number(m_attachIndex);
	}

	ThreadId PostId::getThreadId() const
	{
		return ThreadId(chan(), board(), threadId());
	}

	bool PostId::operator<(const Wishmaster::PostId& id) const
	{
		// FIXME make correct comparison
		return (chan() < id.chan()) ||
			(board() < id.board()) ||
			(threadId() < id.threadId()) ||
			(postId() < id.postId()) ||
			(attachIndex() < id.attachIndex());
	}

	bool PostId::operator==(const Wishmaster::PostId& id1) const
	{
		return (id1.chan() == chan()) &&
			(id1.board() == board()) &&
			(id1.threadId() == threadId()) &&
			(id1.postId() == postId()) &&
			(id1.attachIndex() == attachIndex());
	}

	uint WISHMASTER_EXPORT qHash(const PostId& id)
	{
		return ::qHash(id.chan()) + ::qHash(id.board()) + ::qHash(id.threadId()) + ::qHash(id.postId()) + ::qHash(id.attachIndex());
	}
}

//uint WISHMASTER_EXPORT qHash(const Wishmaster::PostId& id)
//{
//	return qHash(id.chan()) + qHash(id.board()) + qHash(id.threadId()) + qHash(id.postId()) + qHash(id.attachIndex());
//}

//bool WISHMASTER_EXPORT operator==(const Wishmaster::PostId& id1, const Wishmaster::PostId& id2)
//{
//	return (id1.chan() == id2.chan()) &&
//		(id1.board() == id2.board()) &&
//		(id1.threadId() == id2.threadId()) &&
//		(id1.postId() == id2.postId()) &&
//		(id1.attachIndex() == id2.attachIndex());
//}
